package org.jumiz.javafx.client.pages.commonents;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import javafx.geometry.Insets;
import javafx.scene.control.*;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jumiz.javafx.client.util.FileUtil;


/**
 * 布局
 */
public class LayoutPane extends BorderPane {

    private static Logger logger = LogManager.getLogger(LayoutPane.class);

    private JumizStackPane header;
    private JumizStackPane banner;
    private JumizStackPane content;
    private JumizStackPane footer;

    public LayoutPane() {
        super();
        this.init();
    }

    public void init() {
//        header = new JumizStackPane();
//        header.getStyleClass().add("bg-header");
//        header.setPrefHeight(50);
//        header.setPrefWidth(SwitchScene.defaultWidth());
//
//        banner = new JumizStackPane();
//        banner.getStyleClass().add("bg-banner");
//        banner.setPrefWidth(240);
//        banner.setPrefHeight(SwitchScene.defaultHeight() - 100);
//
//        TabPane tabPane = new TabPane();
//        Tab tab1 = new Tab("tab 1");
//        tab1.setClosable(false);
//        StackPane stackPane = new StackPane();
//        tab1.setContent(stackPane);
//        Tab tab2 = new Tab("tab 2");
//        tabPane.getTabs().addAll(tab1, tab2);
//
//        BorderPane borderPane = new BorderPane();
//
//        banner.getChildren().addAll(tabPane);
//
//        content = new JumizStackPane();
//        content.getStyleClass().add("bg-content");
//        content.setPrefWidth(SwitchScene.defaultWidth() - 240);
//        content.setPrefHeight(SwitchScene.defaultHeight() - 100);
//
//        HBox hBox = new HBox();
//        hBox.getChildren().addAll(banner, content);
//        hBox.setPrefHeight(SwitchScene.defaultHeight() - 100);
//        hBox.setPrefWidth(SwitchScene.defaultWidth());
//
//        footer = new JumizStackPane();
//        footer.getStyleClass().add("bg-footer");
//        footer.setPrefWidth(SwitchScene.defaultWidth());
//        footer.setPrefHeight(50);
//
//        VBox vBox = new VBox();
//        vBox.getChildren().addAll(header, hBox, footer);
//        vBox.setPrefWidth(SwitchScene.defaultWidth());
//        vBox.setPrefHeight(SwitchScene.defaultHeight());

//        VBox.setVgrow(header, Priority.ALWAYS);

//        BorderPane pane = new BorderPane();


//        FlowPane flowPane = new FlowPane();
//        flowPane.setAlignment(Pos.CENTER);
//        flowPane.setOrientation(Orientation.VERTICAL);
//        JSONArray bannerArray = JSON.parseArray(bannerJson);
//        for (int i = 0; i < bannerArray.size(); i++) {
//            JSONObject bannerObj = bannerArray.getJSONObject(i);
//            flowPane.getChildren().add(new Button(bannerObj.getString("banner")));
//        }

        CrudTablePane crudTablePane = new CrudTablePane("static/pages/user/curd.json");


        MenuBar menuBar = new MenuBar();
//        menuBar.setStyle("-fx-background-color: pink;");
        Menu file = new Menu("文件");
        file.getItems().add(new Menu("新建"));
        file.getItems().add(new Menu("打开"));
        menuBar.getMenus().add(file);
        menuBar.getMenus().add(new Menu("编辑"));

        String bannerJson = FileUtil.loadJson("static/pages/layout/banner.json");
        JSONArray bannerArray = JSON.parseArray(bannerJson);
        ExtendedTreeItem root = new ExtendedTreeItem<String, JSONObject>();
        for (int i = 0; i < bannerArray.size(); i++) {
            JSONObject bannerObj = bannerArray.getJSONObject(i);
            root.getChildren().add(buildTreeItem(bannerObj));
        }
        TreeView<String> treeView = new TreeView<String>(root);
        treeView.setShowRoot(false);
        treeView.setPrefWidth(200);
        treeView.setPadding(new Insets(5));
        treeView.setOnMouseClicked(event -> {
            if (event.getButton() == MouseButton.PRIMARY) {
                if (treeView.getSelectionModel() != null && treeView.getSelectionModel().getSelectedItem() != null) {
                    logger.debug("点击了 " + ((ExtendedTreeItem) treeView.getSelectionModel().getSelectedItem()).getExtended().toString());
                }
            }
        });


//        TreeItem<String> root = new TreeItem<String>("root");
//        root.getChildren().add(new TreeItem<String>("一级目录"));
//        root.getChildren().add(new TreeItem<String>("二级目录"));
//        root.getChildren().add(new TreeItem<String>("三级目录"));
//        TreeView<String> treeView = new TreeView<String>(root);
//        treeView.setPrefWidth(200);

        setTop(menuBar);
//        setTop(new Label("top"));
        setCenter(crudTablePane);
        setBottom(new Label("bottom"));
        setLeft(treeView);
//        setRight(new Label("right"));
//        setRight(flowPane);
        setPadding(new Insets(5));

//        this.getChildren().addAll(pane);
    }

    @Override
    public LayoutPane clone() {
        return new LayoutPane();
    }

    private ExtendedTreeItem buildTreeItem(JSONObject jsonObject) {
        ExtendedTreeItem<String, JSONObject> treeItem = new ExtendedTreeItem<>(jsonObject.getString("banner"), jsonObject);
        JSONArray children = jsonObject.getJSONArray("children");
        if (children == null || children.size() < 1) {
            return treeItem;
        }
        for (int i = 0; i < children.size(); i++) {
            treeItem.getChildren().add(buildTreeItem(children.getJSONObject(i)));
        }
        return treeItem;
    }
}
